package book.ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DeleteOperations implements IOperations{
    public void work(BookList bookList){
        System.out.println("删除图书...");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的图书名：");
        String name = scanner.nextLine();

        int currentsize = bookList.getUsedSize();
        int pos = -1;
        int i = 0;
        for (; i < currentsize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                pos = i;
                break;
            }
        }
        if(i == currentsize){
            System.out.println("该图书不存在，无法删除！");
            return;
        }

        for(int j = pos; j < currentsize - 1; j++){
            Book book = bookList.getBook(j+1);
            bookList.setBook(j,book);
        }

        bookList.setBook(currentsize-1,null);
        bookList.setUsedSize(currentsize-1);
        System.out.println("《"+name+"》"+"删除成功");
    }
}

